﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class FindbackPassword : Form {
        SystemUser user { get; set; }

        SystemUserManage SystemUserManage { get; set; }
        public FindbackPassword( ) {
            InitializeComponent( );
            SystemUserManage = new SystemUserManage();
        }

        public FindbackPassword(string username):this() {
            user = SystemUserManage.Load(username);
        }

        private void FindbackPassword_Load(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(user.PasswordQuestion1)) {
                FormHelper.ShowErrorMsg("没有设置密码问题，不能找回密码，请联系管理员");
                Close();
            }
            label1.Text = user.PasswordQuestion1;
            label2.Text = user.PasswordQuestion2;
            label3.Text = user.PasswordQuestion3;
           
        }

        private void button1_Click(object sender, EventArgs e) {
            var answer1 = textBox1.Text.Trim();
            var answer2 = textBox2.Text.Trim( );
            var answer3 = textBox3.Text.Trim( );

            if (user.PasswordAnswer1 == answer1 && user.PasswordAnswer2 == answer2 && user.PasswordAnswer3 == answer3) {
                textBox4.Enabled = textBox5.Enabled = button2.Enabled = true;
            }
            else {
                FormHelper.ShowErrorMsg("密码问题答案错误！");
            }

        }

        private void button2_Click(object sender, EventArgs e) {
            var p1 = textBox4.Text.Trim();
            var p2 = textBox5.Text.Trim();
            if (!string.IsNullOrWhiteSpace(p1) && p1 == p2) {
                user.Password = SystemUser.MD5Pwd(p1);
                SystemUserManage.Update(user);
                FormHelper.ShowInfoMsg("修改密码成功，用你的新密码登录吧");
                Close();
            }
            else {
                FormHelper.ShowErrorMsg("请输入新密码，并确保两次输入一致");
            }
            
        }

        private void textBox4_Leave(object sender, EventArgs e) {
            var npwd = textBox4.Text.Trim( );
            if (npwd.Length > 8) {
                label6.Text = "密码长度不能超过8";
                button1.Enabled = false;
            } else if (npwd.Length == 0) {
                label6.Text = "新密码不能为空";
                button2.Enabled = false;
            } else {
                button2.Enabled = true;
            }
        }

        private void textBox5_Leave(object sender, EventArgs e) {
            var cnpwd = textBox5.Text.Trim( );
            var npwd = textBox4.Text.Trim( );
            if (cnpwd != npwd) {
                label7.Text = "两次输入密码不一致";
                button2.Enabled = false;
            } else {
                button2.Enabled = true;
            }
        }


    }
}
